package com.bafa.services.scorers;

import com.bafa.debug.Debug;
import com.bafa.jdbc.DBAccess;
import com.bafa.objects.Match;
import com.bafa.objects.Message;
import com.bafa.objects.player.Player;
import com.bafa.objects.player.PlayerDivision;
import com.bafa.objects.player.PlayerSeason;
import com.bafa.objects.player.PlayerTeam;
import com.bafa.objects.season.Division;
import com.bafa.objects.team.Team;
import com.bafa.objects.team.TeamSeasonStats;
import com.bafa.services.RetrieveServices;

public class FunctionalScorerServices {

	public static void addScorers(DBAccess db, Match match, String[] scorers,
			String[] scorerGoals, Team team, boolean home) throws Exception {
		for (int i = 0; i < scorers.length; i++) {
			
			addScorer(db,match,scorers[i],scorerGoals[i],team,home);
		}
	}

	public static void addScorer(DBAccess db, Match match, String scorer,
			String scorerGoals, Team team, boolean home)throws Exception{
		Debug.debug(new FunctionalScorerServices().getClass(),"addScorer","Entering with......."+scorer+", "+scorerGoals+" and "+match.getID());
		int teamId = 0;
		if (home)
			teamId = match.getTeamA();
		else
			teamId = match.getTeamB();
		Player player = RetrieveServices.getPlayer(scorer);
		Debug.debug(new FunctionalScorerServices().getClass(),"addScorer","Player is "+player);
		if (player == null)
			player = new Player();
		player.setFirstname(scorer);
		player.enterGoals(new Integer(scorerGoals).intValue(), match
				.getSeasonId(), match.getDivision(), teamId);
		player.enterMatchGoals(match.getID(), new Integer(scorerGoals));
		if (home)
			match.addScorersA(scorer, new Integer(scorerGoals)
					.intValue());
		else
			match.addScorersB(scorer, new Integer(scorerGoals)
					.intValue());
		team.getStats().enterScorer(scorer, match.getSeasonId());
		((Division) RetrieveServices.getSeason("" + match.getSeasonId())
				.getDivisions().get(match.getDivision()))
				.enterScorer(scorer);

		db.updatePlayer(player);
		db.updateTeam(team);
		db.updateMatch(match);
		db.updateSeason(RetrieveServices
				.getSeason("" + match.getSeasonId()));
		
		Debug.debug(new FunctionalScorerServices().getClass(),"addScorer",".......Leaving");
	}
	public static Message deleteScorer(String scorerName,  int matchId,int teamId){
		DBAccess db = new DBAccess();
		Message message = new Message();
		try{
			Debug.debug(new FunctionalScorerServices().getClass(),"deleteScorer","Entering.....");
			Debug.debug(new FunctionalScorerServices().getClass(),"deleteScorer","Scorer Name is "+scorerName);
			Match match = RetrieveServices.getMatch(matchId);
			Debug.debug(new FunctionalScorerServices().getClass(),"deleteScorer","Number of A Scorers"+match.getScorersA().size());
			Debug.debug(new FunctionalScorerServices().getClass(),"deleteScorer","Number of B Scorers"+match.getScorersB().size());
			PlayerSeason playerSeason = (PlayerSeason)RetrieveServices.getPlayer(scorerName).getSeasonGoals().get(match.getSeasonId());
			Debug.debug(new FunctionalScorerServices().getClass(),"deleteScorer","Retrieve Player "+RetrieveServices.getPlayer(scorerName));
			PlayerDivision playerDivision = (PlayerDivision)RetrieveServices.getPlayer(scorerName).getDivisionGoals().get(match.getDivision());
			Debug.debug(new FunctionalScorerServices().getClass(),"deleteScorer","Retrieve Division "+playerDivision);
			PlayerTeam playerTeam = (PlayerTeam)RetrieveServices.getPlayer(scorerName).getTeamGoals().get(teamId);
			Debug.debug(new FunctionalScorerServices().getClass(),"deleteScorer","Retrieve Team "+playerTeam);
			PlayerDivision playerSeasonDivision = (PlayerDivision)playerSeason.getDivisionGoals().get(match.getDivision()); 
			PlayerTeam playerSeasonTeam = (PlayerTeam)playerSeason.getTeamGoals().get(teamId);
			
			int goals = RetrieveServices.getPlayer(scorerName).getMatchGoals().get(matchId).getGoals();
			RetrieveServices.getPlayer(scorerName).getMatchGoals().remove(matchId);
			playerSeason.setGoals(playerSeason.getGoals()-goals);
			playerSeasonDivision.setGoals(playerSeasonDivision.getGoals()-goals);
			playerSeasonTeam.setGoals(playerSeasonTeam.getGoals()-goals);
			if(playerSeasonTeam.getGoals()==0){
				playerSeason.getTeamGoals().remove(teamId);
				((TeamSeasonStats)RetrieveServices.getTeam(""+teamId).getStats().getSeasonStats().get(match.getSeasonId())).getScorers().remove(scorerName);
				
			}
			if(playerSeasonDivision.getGoals()==0){
				playerSeason.getDivisionGoals().remove(match.getDivision());
				((Division)RetrieveServices.getSeason(""+match.getSeasonId()).getDivisions().get(match.getDivision())).getScorers().remove(scorerName);
			}
			if(playerSeason.getGoals()==0){
				RetrieveServices.getPlayer(scorerName).getSeasonGoals().remove(match.getSeasonId());
			}
			
			
			if(playerDivision!=null)playerDivision.setGoals(playerDivision.getGoals()-goals);
			playerTeam.setGoals(playerTeam.getGoals()-goals);
			RetrieveServices.getPlayer(scorerName).setTotalGoals(RetrieveServices.getPlayer(scorerName).getTotalGoals()-goals);
			((TeamSeasonStats)RetrieveServices.getTeam(""+teamId).getStats().getSeasonStats().get(match.getSeasonId())).getScorers().remove(scorerName);
			match.getScorersA().remove(scorerName);
			Debug.debug(new FunctionalScorerServices().getClass(),"deleteScorer","Number of A Scorers"+match.getScorersA().size());
			
			match.getScorersB().remove(scorerName);
			
			Debug.debug(new FunctionalScorerServices().getClass(),"deleteScorer","Number of B Scorers"+match.getScorersB().size());
			db.updateMatch(match);
			db.updatePlayer(RetrieveServices.getPlayer(scorerName));
			db.updateTeam(RetrieveServices.getTeam(""+teamId));
			message.addMessage(scorerName+" removed successfully from scorers list");
		
		}catch(Exception e){
			e.printStackTrace();
			message.addError("Failed to delete scorers");
		}finally{
			db.disconnect();
			
		}
		return message;
	}

}
